package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.VipUserRuleBo;
import com.rla.domain.vo.VipUserRuleVo;
import com.rla.domain.VipUserRule;
import com.rla.mapper.VipUserRuleMapper;
import com.rla.service.IVipUserRuleService;

import java.util.*;

/**
 * 会员等级Service业务层处理
 *
 * @author rla
 * @date 2025-08-30
 */
@RequiredArgsConstructor
@Service
public class VipUserRuleServiceImpl implements IVipUserRuleService {

    private final VipUserRuleMapper baseMapper;

    /**
     * 查询会员等级
     */
    @Override
    public VipUserRuleVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public Map<String, Object> getUserRuleInfoByUserId(String userId) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> userRule = baseMapper.queryUserRuleByUserId(userId);
        map.put("userRule", userRule);

        //获取对应会员等级的权益列表
        List<Map<String, Object>> rightsList = baseMapper.queryRightsList(userRule.get("id").toString());
        map.put("rightsList", rightsList);
        return map;
    }

    /**
     * 查询会员等级列表
     */
    @Override
    public TableDataInfo<VipUserRuleVo> queryPageList(VipUserRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VipUserRule> lqw = buildQueryWrapper(bo);
        Page<VipUserRuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会员等级列表
     */
    @Override
    public List<VipUserRuleVo> queryList(VipUserRuleBo bo) {
        LambdaQueryWrapper<VipUserRule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VipUserRule> buildQueryWrapper(VipUserRuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VipUserRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getGrade()), VipUserRule::getGrade, bo.getGrade());
        lqw.eq(StringUtils.isNotBlank(bo.getGradeCode()), VipUserRule::getGradeCode, bo.getGradeCode());
        lqw.eq(StringUtils.isNotBlank(bo.getExpirationDate()), VipUserRule::getExpirationDate, bo.getExpirationDate());
        lqw.eq(bo.getStartTime() != null, VipUserRule::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, VipUserRule::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpgradeWay()), VipUserRule::getUpgradeWay, bo.getUpgradeWay());
        lqw.eq(bo.getStartUpgradeCondition() != null, VipUserRule::getStartUpgradeCondition, bo.getStartUpgradeCondition());
        lqw.eq(bo.getEndUpgradeCondition() != null, VipUserRule::getEndUpgradeCondition, bo.getEndUpgradeCondition());
        lqw.eq(StringUtils.isNotBlank(bo.getCardFaceUrl()), VipUserRule::getCardFaceUrl, bo.getCardFaceUrl());
        return lqw;
    }

    /**
     * 新增会员等级
     */
    @Override
    public Boolean insertByBo(VipUserRuleBo bo) {
        VipUserRule add = BeanUtil.toBean(bo, VipUserRule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会员等级
     */
    @Override
    public Boolean updateByBo(VipUserRuleBo bo) {
        VipUserRule update = BeanUtil.toBean(bo, VipUserRule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VipUserRule entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会员等级
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
